//
//  MPTMeCellItemView.m
//  MPTLive
//
//  Created by caixun on 15/8/25.
//  Copyright (c) 2015年 Jeakin. All rights reserved.
//

#import "MPTMeCellItemView.h"
#import "UIColor+MPTColor.h"
#import "UIImageView+WebCache.h"
#import "MPTTool.h"
#import "UIFont+MPTFont.h"


#pragma mark - ******************************** 公共宏 **********************************
//距离view的底部的距离
#define Footer_Space (6)
//个人中心每一个cell中的内容距离cell顶部的距离
#define Cell_Top_Space (10)
// view中除去视频封面、除去距离cell顶部的距离以外的总高度
#define OtherH (VideoTitleH + LiveTimeH + VideoIcon_VideoTitle_Space + LiveTime_VideoTitle_Space + Footer_Space)

#pragma mark - ******************************** 间距宏 **********************************
// 视频封面与视频标题的距离
#define VideoIcon_VideoTitle_Space (5)
// 直播时间与视频标题的间距
#define LiveTime_VideoTitle_Space (4)

#pragma mark - ******************************** 视频标题宏 **********************************
//视频标题的高度
#define VideoTitleH (17)

#pragma mark - ******************************** 直播时间宏 **********************************
// 直播时间的高度
#define LiveTimeH (14)


@interface MPTMeCellItemView ()

// 视频封面
@property (nonatomic, strong) UIImageView *videoIcon;
// 视频标题
@property (nonatomic, strong) UILabel *videoTitle;
// 视频播放按钮
@property (nonatomic, strong) UIImageView *playImgv;
// 背景按钮
@property (nonatomic, strong) MPTLiveCustomBtn *playBtn;
// 直播时间
@property (nonatomic, strong) UILabel *liveTime;
//
@property (nonatomic, strong) UILabel *userNum;
//
@property (nonatomic, strong) UILabel *likeNum;
//
@property (nonatomic, strong) UIImageView *userNumImgv;
//
@property (nonatomic, strong) UIImageView *likeNumImgv;


@end


@implementation MPTMeCellItemView


- (void)initViewContent
{
    self.backgroundColor = [UIColor whiteColor];
    
    // 视频封面
    self.videoIcon = [[UIImageView alloc] init];
    self.videoIcon.contentMode = UIViewContentModeScaleAspectFill;
    [self addSubview:self.videoIcon];
    
    // 视频标题
    self.videoTitle = [[UILabel alloc] init];
    self.videoTitle.font = [UIFont YXFontOfSize:15.0f];
    [self.videoTitle setTextColor:[UIColor YXColorWithHexCode:@"#23232B"]];
    [self addSubview:self.videoTitle];
    
    // 视频播放按钮
    UIImage *play = [UIImage imageNamed:@"MPTLive_Play"];
    self.playImgv = [[UIImageView alloc] init];
    self.playImgv.image = play;
    [self addSubview:self.playImgv];
    
    // 背景按钮
    self.playBtn = [MPTLiveCustomBtn buttonWithType:UIButtonTypeCustom];
    [self addSubview:self.playBtn];
    
    // 直播时间
    self.liveTime = [[UILabel alloc] init];
    self.liveTime.font = [UIFont YXFontOfSize:9.0f];
    self.liveTime.textColor = [UIColor YXColorWithHexCode:@"#d2d2d2"];
    [self addSubview:self.liveTime];
    
    // 观看视频用户数量
    self.userNum = [[UILabel alloc] init];
    self.userNum.font = [UIFont YXFontOfSize:10.0f];
    self.userNum.textColor = [UIColor YXColorWithHexCode:@"#999999"];
    [self addSubview:self.userNum];
    
    //观看视频用户数量图片
    UIImage *userNumImg = [UIImage imageNamed:@"MPTLive_Me_UserNumIcon"];
    self.userNumImgv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, userNumImg.size.width, userNumImg.size.height)];
    self.userNumImgv.image = userNumImg;
    [self addSubview:self.userNumImgv];
    
    // 点赞数量
    self.likeNum = [[UILabel alloc] init];
    self.likeNum.font = [UIFont YXFontOfSize:10.0f];
    self.likeNum.textColor = [UIColor YXColorWithHexCode:@"#999999"];
    [self addSubview:self.likeNum];
    
    //点赞数量图片
    UIImage *likeNumImg = [UIImage imageNamed:@"MPTLive_Me_LikeNumIcon"];
    self.likeNumImgv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, likeNumImg.size.width, likeNumImg.size.height)];
    self.likeNumImgv.image = likeNumImg;
    [self addSubview:self.likeNumImgv];
}

- (void)setViewContent:(MeVideoModel *)videoModel WithTarget:(id)target withAction:(SEL)playAction
{
    // 视频封面
    CGRect videoIconRect = CGRectMake(0,
                                      0,
                                      self.frame.size.width,
                                      self.frame.size.height-OtherH);
    self.videoIcon.frame = videoIconRect;
    [self.videoIcon sd_setImageWithURL:[NSURL URLWithString:videoModel.videoIcon] placeholderImage:nil];
    
    // 视频标题
    CGRect titleRect = CGRectMake(5,
                                  self.videoIcon.frame.origin.y+self.videoIcon.frame.size.height+VideoIcon_VideoTitle_Space,
                                  self.frame.size.width,
                                  VideoTitleH);
    self.videoTitle.frame = titleRect;
    self.videoTitle.text = videoModel.videoTitle;
    
    // 视频播放按钮
    UIImage *play = [UIImage imageNamed:@"MPTLive_Play"];
    self.playImgv.frame = CGRectMake(5, self.videoIcon.frame.origin.y+self.videoIcon.frame.size.height-play.size.height-5, play.size.width, play.size.height);
    self.playImgv.image = play;
    
    // 背景按钮
    self.playBtn.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    [self.playBtn addTarget:target action:playAction forControlEvents:UIControlEventTouchUpInside];
    self.playBtn.paramDic = [NSMutableDictionary dictionary];
    [self.playBtn.paramDic setObject:videoModel forKey:@"videoModel"];
    
    // 直播时间
    float w = [self boundingRectWithSize:videoModel.videoTime withFontSize:9.0f];
    CGRect timeRect = CGRectMake(self.frame.size.width-5-w,
                                 self.videoTitle.frame.origin.y+self.videoTitle.frame.size.height+LiveTime_VideoTitle_Space,
                                 w,
                                 LiveTimeH);
    self.liveTime.frame = timeRect;
    self.liveTime.text = [NSString stringWithFormat:@"%@",videoModel.videoTime];
    
    // 观看视频用户数量图片
    self.userNumImgv.frame = CGRectMake(5, self.liveTime.frame.origin.y, self.userNumImgv.frame.size.width, self.userNumImgv.frame.size.height);
    self.userNumImgv.center = CGPointMake(self.userNumImgv.center.x, self.liveTime.center.y);
    
    // 观看视频用户数量
    float userNumW = [self boundingRectWithSize:videoModel.ucnt withFontSize:10.0f];
    self.userNum.text = [MPTTool getNumWith:videoModel.ucnt.integerValue];
    self.userNum.frame = CGRectMake(self.userNumImgv.frame.origin.x+self.userNumImgv.frame.size.width+5, self.userNumImgv.frame.origin.y, userNumW, 12);
    self.userNum.center = CGPointMake(self.userNum.center.x, self.userNumImgv.center.y);
    
    // 点赞数量图片
    self.likeNumImgv.frame = CGRectMake(self.userNum.frame.origin.x+self.userNum.frame.size.width+10, self.userNumImgv.frame.origin.y, self.likeNumImgv.frame.size.width, self.likeNumImgv.frame.size.height);
    self.likeNumImgv.center = CGPointMake(self.likeNumImgv.center.x, self.liveTime.center.y);
    
    // 点赞数量
    float likeNumW = [self boundingRectWithSize:videoModel.mcnt withFontSize:10.0f];
    self.likeNum.text = [MPTTool getNumWith:videoModel.mcnt.integerValue];
    self.likeNum.frame = CGRectMake(self.likeNumImgv.frame.origin.x+self.likeNumImgv.frame.size.width+5, self.userNumImgv.frame.origin.y, likeNumW, 12);
    self.likeNum.center = CGPointMake(self.likeNum.center.x, self.likeNumImgv.center.y);
}


- (float)boundingRectWithSize:(NSString *)content withFontSize:(float)fontSize
{
    UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, MAXFLOAT, 12)];
    tempLabel.font = [UIFont YXFontOfSize:fontSize];
    tempLabel.text = content;
    [tempLabel sizeToFit];
    
    return tempLabel.frame.size.width;
}


@end
